package com.adgox.tiantianbiting.mine.mobile;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.adgox.tiantianbiting.R;
import com.adgox.tiantianbiting.bean.BaseResponseBean;
import com.adgox.tiantianbiting.bean.UserInfo;
import com.adgox.tiantianbiting.main.MainActivity;
import com.adgox.tiantianbiting.net.callback.JsonCallback;
import com.adgox.tiantianbiting.utils.ApiConstant;
import com.adgox.tiantianbiting.utils.DialogUtils;
import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;
import com.rae.swift.session.SessionManager;

import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class BindMobileActivity extends AppCompatActivity {

    @BindView(R.id.et_mobile)
    EditText etMobile;
    @BindView(R.id.et_ver)
    EditText etVer;
    @BindView(R.id.tv_send)
    TextView tvSend;
    private int userId;
    private int vertifyCD;
    private String mobile;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bind_mobile);
        ButterKnife.bind(this);
        UserInfo user = SessionManager.getDefault().getUser();
        userId = user.getId();
    }

    @OnClick(R.id.img_finish)
    public void onFinishClicked() {
        finish();
    }

    @OnClick(R.id.tv_send)
    public void onSendClicked() {
        mobile = etMobile.getText().toString().trim();
        if (!mobile.matches("[1]\\d{10}")){
            Toast.makeText(this, "手机号格式不对", Toast.LENGTH_SHORT).show();
            return;
        }
        //发送验证码
        OkGo.<BaseResponseBean<String>>post(ApiConstant.BASE_URL + ApiConstant.GET_VERTIFY_CODE + mobile + "/" + userId)
                .params("type", "login")
                .execute(new JsonCallback<BaseResponseBean<String>>() {
                    @Override
                    public void onSuccess(Response<BaseResponseBean<String>> response) {

                        if ("0".equals(response.body().getCode())) {
                            Toast.makeText(BindMobileActivity.this, "获取验证码成功", Toast.LENGTH_SHORT).show();
                            tvSend.setClickable(false);
                            vertifyCD = 60;
                            handler.post(runnable);
                        } else {
                            Toast.makeText(BindMobileActivity.this, response.body().getMessage(), Toast.LENGTH_SHORT).show();
                        }
                    }
                });

    }

    @OnClick(R.id.tv_bind)
    public void onBindClicked() {
        showLoading();
        if (mobile == null || !mobile.equals(etMobile.getText().toString().trim())){
            Toast.makeText(this, "手机号为空或前后不一致", Toast.LENGTH_SHORT).show();
            return;
        }

        String verCode = etVer.getText().toString().trim();
        if (verCode == null){
            Toast.makeText(this, "验证码不可为空", Toast.LENGTH_SHORT).show();
            return;
        }

        UserInfo user = SessionManager.getDefault().getUser();
        Gson gson = new Gson();
        HashMap<String, Object> map = new HashMap<>();
        map.put("mobile",mobile);
        map.put("userId",user.getId());
        map.put("verCode", verCode);
        String s = gson.toJson(map);
        OkGo.<BaseResponseBean<String>>post(ApiConstant.BASE_URL+ApiConstant.CHECK_VERTIFY_CODE)
                .upJson(s)
                .execute(new JsonCallback<BaseResponseBean<String>>() {
                    @Override
                    public void onSuccess(Response<BaseResponseBean<String>> response) {
                        DialogUtils.dismiss();
                        if ("0".equals(response.body().getCode())){
                            Toast.makeText(BindMobileActivity.this, "绑定成功", Toast.LENGTH_SHORT).show();
                            toMain();
                        } else {
                            Toast.makeText(BindMobileActivity.this, response.body().getMessage(), Toast.LENGTH_SHORT).show();
                        }
                    }

                    @Override
                    public void onError(Response<BaseResponseBean<String>> response) {
                        super.onError(response);

                    }
                });
    }

    private void toMain() {
        startActivity(new Intent(BindMobileActivity.this, MainActivity.class));
    }

    @OnClick(R.id.tv_cancel)
    public void onCancelClicked() {
        finish();
    }

    Handler handler = new Handler();
    Runnable runnable = new Runnable() {
        @Override
        public void run() {
            vertifyCD--;
            tvSend.setText(vertifyCD+"s");
            tvSend.setTextColor(getResources().getColor(R.color.gray));
            if (vertifyCD == 0) {
                tvSend.setText("获取验证码");
                tvSend.setTextColor(getResources().getColor(R.color.black));
                tvSend.setClickable(true);
                handler.removeCallbacks(runnable);
            } else {
                handler.postDelayed(runnable, 1000);
            }
        }
    };

    public void showLoading(){
        DialogUtils.showLoadingDialog(this);
    }
}
